/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.action;

import java.io.Serializable;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;


/**
 * 
 * @author Thibaut CASELLI
 * 
 * @param <B>
 *           the type of the beans returned in the {@link PagingLoadResult}
 * @param <C>
 *           the type of the given {@link PagingLoadConfig}
 * @param <D>
 *           the type of the RPC returned {@link PagingLoadResult}
 */
public class DkPagingLoadAction<B extends Serializable, C extends PagingLoadConfig, D extends PagingLoadResult<B>> extends
		DkBaseAction<D> implements HasProxyAsyncCallback
{

	final DkBasePagingLoader<B, C, D> pagingLoader;
	private AsyncCallback<D> proxyCallback;
	private boolean resetOffset;

	/**
	 * Constructor
	 * 
	 * @param pagingLoader
	 */
	public DkPagingLoadAction(final DkBasePagingLoader<B, C, D> pagingLoader)
	{
		this(null, false, pagingLoader);
	}

	/**
	 * Constructor
	 * 
	 * @param progressMessage
	 * @param pagingLoader
	 */
	public DkPagingLoadAction(final String progressMessage, final DkBasePagingLoader<B, C, D> pagingLoader)
	{
		this(progressMessage, false, pagingLoader);
	}

	/**
	 * Constructor
	 * 
	 * @param progressMessage
	 * @param showProgress
	 * @param pagingLoader
	 */
	public DkPagingLoadAction(final String progressMessage, final boolean showProgress,
			final DkBasePagingLoader<B, C, D> pagingLoader)
	{
		super(progressMessage, showProgress);
		this.pagingLoader = pagingLoader;
	}

	/**
	 * Constructor
	 * 
	 * @param progressMessage
	 * @param showProgress
	 * @param pagingLoader
	 * @param resetOffset
	 *           to reset offset and reload from 0
	 */
	public DkPagingLoadAction(final String progressMessage, final boolean showProgress,
			final DkBasePagingLoader<B, C, D> pagingLoader, final boolean resetOffset)
	{
		super(progressMessage, showProgress);
		this.pagingLoader = pagingLoader;
		this.resetOffset = resetOffset;
	}

	/**
	 * Load called from action.execute()
	 */
	@Override
	protected void run()
	{
		pagingLoader.loadFromAction(this);
	}

	/**
	 * Load method called by the proxy
	 * 
	 * @param loadConfig
	 *           the load config
	 * @param callback
	 *           the callback
	 */
	public void load(final C loadConfig, final AsyncCallback<D> callback)
	{
		this.proxyCallback = callback;
		if (resetOffset)
		{
			loadConfig.setOffset(0);
		}
		pagingLoader.doLoad(loadConfig);
	}

	@SuppressWarnings("unchecked")
	@Override
	public AsyncCallback<D> getProxyAsyncCallback()
	{
		return proxyCallback;
	}

}
